home *** CD-ROM | disk | FTP | other *** search
/ Clickx 23 / Clickx 23.iso / DATA / BLENDE~1.ZIP / blender-2.37a-OSX-10.2-powerpc / plugins / sequence / blur.c < prev    next >
Encoding:
C/C++ Source or Header  |  2005-06-15  |  5.1 KB  |  244 lines

  1. /**
  2.  * $Id: blur.c,v 1.1 2003/01/01 15:06:08 hos Exp $
  3.  *
  4.  * ***** BEGIN GPL/BL DUAL LICENSE BLOCK *****
  5.  *
  6.  * This program is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU General Public License
  8.  * as published by the Free Software Foundation; either version 2
  9.  * of the License, or (at your option) any later version. The Blender
  10.  * Foundation also sells licenses for use in proprietary software under
  11.  * the Blender License.  See http://www.blender.org/BL/ for information
  12.  * about this.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software Foundation,
  21.  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  22.  *
  23.  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  24.  * All rights reserved.
  25.  *
  26.  * The Original Code is: all of this file.
  27.  *
  28.  * Contributor(s): none yet.
  29.  *
  30.  * ***** END GPL/BL DUAL LICENSE BLOCK *****
  31.  */
  32.  
  33. #include "plugin.h"
  34.  
  35. /* ******************** GLOBAL VARIABLES ***************** */
  36.  
  37.  
  38. char name[24]= "Blur";    
  39.  
  40. /* structure for buttons, 
  41.  *  butcode      name           default  min  max  0
  42.  */
  43.  
  44. VarStruct varstr[]= {
  45.     LABEL,            "Input: 1 strip", 0.0, 0.0, 0.0, "", 
  46.      NUMSLI|FLO,        "Blur",        0.5,    0.0,    10.0, "Maximum filtersize", 
  47.     NUMSLI|FLO,        "Gamma",    1.0,    0.4,    2.0, "Gamma correction", 
  48.     TOG|INT,        "Animated",    0.0,    0.0,    1.0, "For (Ipo) animated blur", 
  49. };
  50.  
  51. /* The cast struct is for input in the main doit function
  52.    Varstr and Cast must have the same variables in the same order */ 
  53.  
  54. typedef struct Cast {
  55.     int dummy;            /* because of the 'label' button */
  56.     float blur;
  57.     float gamma;
  58.     float use_ipo;
  59. } Cast;
  60.  
  61.  
  62. /* cfra: the current frame */
  63.  
  64. float cfra;
  65.  
  66. void plugin_seq_doit(Cast *, float, float, int, int, ImBuf *, ImBuf *, ImBuf *, ImBuf *);
  67.  
  68. /* ******************** Fixed functions ***************** */
  69.  
  70. int plugin_seq_getversion(void) 
  71. {
  72.     return B_PLUGIN_VERSION;
  73. }
  74.  
  75. void plugin_but_changed(int but) 
  76. {
  77. }
  78.  
  79. void plugin_init()
  80. {
  81. }
  82.  
  83. void plugin_getinfo(PluginInfo *info)
  84. {
  85.     info->name= name;
  86.     info->nvars= sizeof(varstr)/sizeof(VarStruct);
  87.     info->cfra= &cfra;
  88.  
  89.     info->varstr= varstr;
  90.  
  91.     info->init= plugin_init;
  92.     info->seq_doit= (SeqDoit) plugin_seq_doit;
  93.     info->callback= plugin_but_changed;
  94. }
  95.  
  96.  
  97. void blurbuf(struct ImBuf *ibuf, int nr, Cast *cast)
  98. {
  99.     /* nr = number of blurs */
  100.     /* the ibuf->rect is replaced */
  101.     struct ImBuf *tbuf, *ttbuf;
  102.     int i, x4;
  103.     
  104.     tbuf= dupImBuf(ibuf);
  105.     x4= ibuf->x/4;
  106.     
  107.     if(cast->gamma != 1.0) gamwarp(tbuf, cast->gamma);
  108.  
  109.     /* reduce */
  110.     for(i=0; i<nr; i++) {
  111.         ttbuf = onehalf(tbuf);
  112.         if (ttbuf) {
  113.             freeImBuf(tbuf);
  114.             tbuf = ttbuf;
  115.         }
  116.         if(tbuf->x<4 || tbuf->y<4) break;
  117.     }
  118.     
  119.     /* enlarge */
  120.     for(i=0; i<nr; i++) {
  121.         ttbuf = double_x(tbuf);
  122.         if (ttbuf) {
  123.             freeImBuf(tbuf);
  124.             tbuf = ttbuf;
  125.         }
  126.         ttbuf = double_y(tbuf);
  127.         if (ttbuf) {
  128.             freeImBuf(tbuf);
  129.             tbuf = ttbuf;
  130.         }
  131.         
  132.         if(tbuf->x > x4) {
  133.             scaleImBuf(tbuf, ibuf->x, ibuf->y);
  134.             break;
  135.         }
  136.     }
  137.     
  138.     if(cast->gamma != 1.0) gamwarp(tbuf, 1.0 / cast->gamma);
  139.  
  140.     freeN(ibuf->rect);
  141.     ibuf->rect= tbuf->rect;
  142.     freeN(tbuf);
  143.     
  144. }
  145.  
  146. void doblur(struct ImBuf *mbuf, float fac, Cast *cast)
  147. {
  148.     /* make two filtered images, like a mipmap structure
  149.      * fac is filtersize in pixels
  150.      */
  151.     struct ImBuf *ibuf, *pbuf;
  152.     float ifac, pfac;
  153.     int n, b1, b2;
  154.     char *irect, *prect, *mrect;
  155.     
  156.     /* wich buffers ? */
  157.                 
  158.     if(fac>7.0) fac= 7.0;
  159.     if(fac<=1.0) return;
  160.     
  161.     pfac= 2.0;
  162.  
  163.     pbuf= dupImBuf(mbuf);
  164.     n= 1;
  165.     while(pfac < fac) {
  166.         blurbuf(pbuf, n, cast);
  167.         blurbuf(pbuf, n, cast);
  168.         
  169.         n++;
  170.         pfac+= 1.0;
  171.     }
  172.  
  173.     ifac= pfac;
  174.     pfac-= 1.0;
  175.  
  176.     ibuf= dupImBuf(pbuf);
  177.     blurbuf(ibuf, n, cast);
  178.     blurbuf(ibuf, n, cast);
  179.     
  180.     fac= 255.0*(fac-pfac)/(ifac-pfac);
  181.     b1= fac;
  182.     if(b1>255) b1= 255;
  183.     b2= 255-b1;
  184.     
  185.     if(b1==255) {
  186.         memcpy(mbuf->rect, ibuf->rect, 4*ibuf->x*ibuf->y);
  187.     }
  188.     else if(b1==0) {
  189.         memcpy(mbuf->rect, pbuf->rect, 4*pbuf->x*pbuf->y);
  190.     }
  191.     else {    /* interpolate */
  192.         n= ibuf->x*ibuf->y;
  193.         irect= (char *)ibuf->rect;
  194.         prect= (char *)pbuf->rect;
  195.         mrect= (char *)mbuf->rect;
  196.         while(n--) {
  197.             mrect[0]= (irect[0]*b1+ prect[0]*b2)>>8;
  198.             mrect[1]= (irect[1]*b1+ prect[1]*b2)>>8;
  199.             mrect[2]= (irect[2]*b1+ prect[2]*b2)>>8;
  200.             mrect[3]= (irect[3]*b1+ prect[3]*b2)>>8;
  201.             mrect+= 4;
  202.             irect+= 4;
  203.             prect+= 4;
  204.         }
  205.     }
  206.     freeImBuf(ibuf);
  207.     freeImBuf(pbuf);
  208. }
  209.  
  210.  
  211. void plugin_seq_doit(Cast *cast, float facf0, float facf1, int x, int y, ImBuf *ibuf1, ImBuf *ibuf2, ImBuf *out, ImBuf *use)
  212. {
  213.     float  bfacf0, bfacf1;
  214.     
  215.     if(cast->use_ipo==0) {
  216.         bfacf0= bfacf1= cast->blur+1.0;
  217.     }
  218.     else {
  219.         bfacf0 = (facf0 * 6.0) + 1.0;
  220.         bfacf1 = (facf1 * 6.0) + 1.0;
  221.     }
  222.  
  223.     memcpy(out->rect, ibuf1->rect, 4*out->x*out->y);
  224.  
  225.     /* it blurs interlaced, only tested with even fields */
  226.     de_interlace(out);
  227.  
  228.     /* otherwise scaling goes wrong */
  229.     out->flags &= ~IB_fields;
  230.     
  231.     doblur(out, bfacf0, cast); /*fieldA*/
  232.  
  233.     out->rect += out->x * out->y;
  234.  
  235.     doblur(out, bfacf1, cast); /*fieldB*/
  236.  
  237.     out->rect -= out->x * out->y;
  238.     out->flags |= IB_fields;
  239.  
  240.     interlace(out);
  241.     
  242. }
  243.  
  244.